﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Game1942.Library.Background;

namespace Game1942.Library.FlightPlans
{
    class EscapePlan : FlightPlan
    {
        private int xInc , yInc;
        private Point actualPosition;
        

		// Constructor, expects the original position and the dimensions for the increases
        public EscapePlan(int speed): base(speed)
        {
            SetIncreases();
        }

        public EscapePlan(Point actualPosition)
        {
            this.actualPosition = actualPosition;
            SetIncreases();
        }

        // Sustained increases in defined positions X and Y
        private void SetIncreases()
        {
            List<int> numberList = new List<int>();

            numberList.Add(actualPosition.X);
            numberList.Add(Background.Background.Instance.GetDimension().X - actualPosition.X);
            numberList.Add(actualPosition.Y);
            numberList.Add(Background.Background.Instance.GetDimension().Y - actualPosition.Y);

            var minDistance = numberList.Min();
            int minDistancePosition = 0;

            for (int i = 0; i <= numberList.LongCount(); i++)
            {
                if (numberList[i].Equals(minDistance))
                {
                    minDistancePosition = i;
                    break;
                }
            }

            switch (minDistancePosition)
            {
                case 0:
                    xInc = -1;break;
                case 1:
                    xInc = 1;break;
                case 2:
                    yInc = -1; break;
                case 3:
                    yInc = 1; break;
            }
        }


        // Get the next point on the trajectory of the flight plan
        public override Point GetNext()
        {
            actualPosition = new Point(actualPosition.X + xInc, actualPosition.Y + yInc);
            return actualPosition;
        }
    }
}
